/*
 * @Author: jianfanfan 1217572833@qq.com
 * @Date: 2025-03-19 17:43:18
 * @LastEditors: jianfanfan 1217572833@qq.com
 * @LastEditTime: 2025-03-26 19:21:15
 * @FilePath: \guozaoke\src\node\node.controller.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { 
  Controller, 
  Get, 
  Post, 
  Body, 
  Param, 
  Headers,
  Query,
  HttpStatus,
  HttpCode,
  ParseIntPipe
} from '@nestjs/common';
import { NodeService } from './node.service';
import { PostsService } from './post.service';
import { CreateNodeDto } from './dto/create-node.dto';
import { CreatePostDto } from './dto/create-post.dto';
import {
  ApiTags,
  ApiOperation,
  ApiParam,
  ApiBody,
  ApiBearerAuth,
  ApiResponse,
  ApiQuery
} from '@nestjs/swagger';
import { ApiResponseDto } from '../common/dto/api-response.dto';

@ApiTags('Node')
@Controller('nodes')
export class NodeController {
  constructor(
    private readonly nodeService: NodeService,
    private readonly postsService: PostsService,
  ) {}

  @Get()
  @ApiOperation({ summary: '获取节点列表', description: '获取所有节点或指定父节点的子节点' })
  @ApiQuery({ name: 'parentId', description: '父节点ID，不提供则获取顶级节点', required: false, type: Number })
  @ApiResponse({ status: HttpStatus.OK, description: '成功获取节点列表', type: ApiResponseDto })
  async getNodes(@Query('parentId') parentId?: number) {
    if (parentId) {
      return this.nodeService.getChildrenNodes(parentId);
    } else {
      return this.nodeService.getTopNodes();
    }
  }

  @Get('tree')
  @ApiOperation({ summary: '获取节点树', description: '获取完整的节点层级结构' })
  @ApiResponse({ status: HttpStatus.OK, description: '成功获取节点树', type: ApiResponseDto })
  async getNodeTree() {
    return this.nodeService.getNodeTree();
  }

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: '创建节点', description: '创建新的节点' })
  @ApiBody({ type: CreateNodeDto, description: '节点创建数据' })
  @ApiResponse({ status: HttpStatus.CREATED, description: '节点创建成功', type: ApiResponseDto })
  async createNode(@Body() createNodeDto: CreateNodeDto) {
    return this.nodeService.createNode(createNodeDto, createNodeDto.parentId);
  }

  @Get(':id/posts')
  @ApiOperation({ summary: '获取节点帖子', description: '获取指定节点下的所有帖子' })
  @ApiParam({ name: 'id', description: '节点ID' })
  @ApiResponse({ status: HttpStatus.OK, description: '成功获取节点帖子列表', type: ApiResponseDto })
  async getPostsByNodeId(@Param('id', ParseIntPipe) id: number) {
    return this.nodeService.getPostsByNodeId(id);
  }

  @Post(':id/posts')
  @HttpCode(HttpStatus.CREATED)
  @ApiBearerAuth('bearer')
  @ApiOperation({ summary: '创建帖子', description: '在指定节点下创建新帖子' })
  @ApiParam({ name: 'id', description: '节点ID' })
  @ApiBody({ type: CreatePostDto })
  @ApiResponse({ status: HttpStatus.CREATED, description: '帖子创建成功', type: ApiResponseDto })
  async createPost(
    @Param('id', ParseIntPipe) nodeId: number,
    @Body() createPostDto: CreatePostDto,
    @Headers('authorization') token: string,
  ) {
    // 确保帖子正确关联到节点
    createPostDto.nodeId = nodeId;
    return this.postsService.createPost(createPostDto, token);
  }

  @Get(':id/children')
  @ApiOperation({ summary: '获取子节点', description: '获取指定节点的直接子节点' })
  @ApiParam({ name: 'id', description: '父节点ID' })
  @ApiResponse({ status: HttpStatus.OK, description: '成功获取子节点列表', type: ApiResponseDto })
  async getChildrenNodes(@Param('id', ParseIntPipe) id: number) {
    return this.nodeService.getChildrenNodes(id);
  }
}
